package com.tsingteng.cosfun.ui.found;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.helper.ActivityManagers;
import com.tsingteng.cosfun.ui.found.adapter.FoundVideoPlayAdapter;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.utils.NotchUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动视频activity
 * author :zht
 */
public class FoundJoinActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager mFoundJoinVp;
    private List<Pair<String, Fragment>> mFragmentPair;
    private int currentPage = 0;
    private FoundVideoPlayFragment mFoundVideoPlayFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (null != savedInstanceState) {
            savedInstanceState.remove(Config.FRAGMENTS_TAG);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found_join);
        ActivityManagers.getInstance().addFoundActivity(this);
        NotchUtil.setWindowTrans(this, true, false);
        initView();
    }

    void initView() {
        String json = getIntent().getStringExtra(Constant.COVERIMGURL);
        Gson gson = new Gson();
        List<OpusBean> coverImgUrl = gson.fromJson(json, new TypeToken<List<OpusBean>>() {
        }.getType());
//        List<FoundJoinBean> coverImgUrl = (List<FoundJoinBean>) getIntent().getSerializableExtra(Constant.COVERIMGURL);
        int curretPosition = getIntent().getIntExtra(Constant.POSITION, 0);
        mFoundJoinVp = findViewById(R.id.found_join_vp);
        mFragmentPair = new ArrayList<>();
        mFoundVideoPlayFragment = new FoundVideoPlayFragment();
        mFoundVideoPlayFragment.setDataStype(coverImgUrl, curretPosition);
        HomeMineFragment mHomeMineFragment = new HomeMineFragment();
        mHomeMineFragment.setBackLisenter(backLisenter);
        Pair mFoundActivity = new Pair("foundVideo", mFoundVideoPlayFragment);
        Pair mHomeFound = new Pair("personal", mHomeMineFragment);
        mFragmentPair.add(mFoundActivity);
        mFragmentPair.add(mHomeFound);
        FoundVideoPlayAdapter mFoundVideoPlayAdapter = new FoundVideoPlayAdapter(getSupportFragmentManager(), mFragmentPair);
        mFoundJoinVp.setAdapter(mFoundVideoPlayAdapter);
        mFoundJoinVp.setCurrentItem(currentPage);
        mFoundJoinVp.setOffscreenPageLimit(2);
        mFoundJoinVp.addOnPageChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (currentPage == 0) {
            if (mFragmentPair.get(currentPage).first.equals("foundVideo")) {
                ((FoundVideoPlayFragment) (mFragmentPair.get(currentPage).second)).onPlay();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (currentPage == 1) {
            if (mFragmentPair.get(currentPage).first.equals("foundVideo")) {
                ((FoundVideoPlayFragment) (mFragmentPair.get(currentPage).second)).onPause();
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
        switch (currentPage) {
            case 0:
                if (mFragmentPair.get(0).first.equals("foundVideo")) {
                    ((FoundVideoPlayFragment) (mFragmentPair.get(0).second)).play();
                }
                break;
            case 1:
                if (mFragmentPair.get(0).first.equals("foundVideo")) {
                    ((FoundVideoPlayFragment) (mFragmentPair.get(0).second)).onPause();
                }
                if (mFragmentPair.get(1).first.equals("personal")) {
                    ((HomeMineFragment) (mFragmentPair.get(1).second)).refreshData();
                    ((HomeMineFragment) (mFragmentPair.get(1).second)).goneButtomView(View.GONE);
                }
                break;
            case 2:
                if (mFragmentPair.get(0).first.equals("foundVideo")) {
                    ((FoundVideoPlayFragment) (mFragmentPair.get(0).second)).onPause();
                }
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    HomeMineFragment.BackLisenter backLisenter = new HomeMineFragment.BackLisenter() {
        @Override
        public void back() {
            mFoundJoinVp.setCurrentItem(0);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (currentPage == 1) {
                    mFoundJoinVp.setCurrentItem(0);
                    return true;
                }
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mFoundVideoPlayFragment.onFragmentTriggerResult(requestCode,resultCode,data);
    }
    public interface OnFragmentTriggerResult {
        void onFragmentTriggerResult(int requestCode, int resultCode, Intent data);
    }

}
